package com.sky.service.impl;

import com.sky.entity.Merchant;
import com.sky.mapper.ShopMapper;
import com.sky.service.ShopService;
import com.sky.vo.ShopVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ShopServiceImpl implements ShopService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private ShopMapper shopMapper;

    private final static String SHOP_STATUS = "shopStatus";

    // 设置营业状态
    @Override
    public void setShopStatus(Integer status) {
        redisTemplate.opsForValue().set(SHOP_STATUS,String.valueOf(status));
        log.info("已加入到Redis缓存中");
    }

    // 管理端营业状态
    @Override
    public Integer manageShopStatus() {
        String s = redisTemplate.opsForValue().get(SHOP_STATUS);
        if(!s.isEmpty()){
            return Integer.valueOf(s);
        }
        return -1;
    }

    // 用户端营业状态
    @Override
    public Integer manageUserStatus() {
        String s = redisTemplate.opsForValue().get(SHOP_STATUS);
        if (!s.isEmpty()){
            return Integer.valueOf(s);
        }
        return -1;
    }

    // 获取商家信息
    @Override
    public ShopVO getMerchantInfo() {
        Merchant merchant = shopMapper.selectShoptById(1L);
        ShopVO shopVO = ShopVO.builder()
                .shopAddress(merchant.getShopAddress())
                .shopName(merchant.getShopName())
                .phone(merchant.getShopTelephone())
                .build();
        return shopVO;
    }


}
